<?php
/**
 * 蜗牛博客
 * 本文件内代码属于“蜗牛博客”项目，禁止修改再发布本程序源码，禁止去除页面底部的版权申明。所有版权保留
 * User: upliu
 * Email: 753073861@qq.com
 * QQ: 753073861
 * 程序主页：http://demo.upliu.net/snail-blog/
 */

require __DIR__.'/init.php';

check_login();

$db = Db::getInstance();
switch (get('action')) {
    case 'save':
        it_must_post();
        if ($id = post('id')) {
            $db->update('tag', array(
                'name' => post('name'),
            ), 'id=?', $id);
            $msg = '保存标签成功';
        } else {
            $db->insert('tag', array(
                'name' => $_POST['name'],
            ));
            $msg = '添加新标签成功';
        }
        redirect_path('tag.php', $msg);
        break;


    case 'delete':
        it_must_post();
        $id = get('id');
        $db->delete('tag', 'id=?', $id);
        $db->delete('map_post_tag', 'tag_id=?', $id);
        redirect_path('tag.php', "删除标签成功");
        break;


    case 'add':
    case 'edit':
        if ($id = get("id")) {
            $tag = $db->query('select * from tag where id=?', $id);
        } else {
            $tag = array();
        }
        if ($pid = get('pid')) {
            $tag['pid'] = $pid;
        }
        echo renderWithLayout('tag-form', array('tag' => $tag));
        break;


    default:
        $tags = $db->queryAll("select * from tag");
        echo renderWithLayout('tag', array(
            'tags' => $tags,
        ));
        break;
}
